#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Flynn.Core;

namespace Flynn.Samples.Versionings.SqlServer
{
	[Versioning("SqlServerVersionsTable")]
	public class VersionsTableVersioning : IVersioning
	{
		public void PrepareDatabase(IDbConnection connection)
		{
			if (connection == null)
				throw new ArgumentNullException("connection");

			using (SqlCommand command = (SqlCommand) connection.CreateCommand())
			{
				command.CommandText = GetCreateVersionsTableCommand();

				command.ExecuteNonQuery();
			}

			using (SqlCommand command = (SqlCommand) connection.CreateCommand())
			{
				command.CommandText = GetInsertInitialVersionCommand();

                command.ExecuteNonQuery();
			}
		}

		public string GetCurrentVersion(IDbConnection connection)
		{
			if (connection == null)
				throw new ArgumentNullException("connection");

			// TODO Do we have to cast here? Is there a way to solve this with generics?
			using (SqlCommand command = ((SqlConnection) connection).CreateCommand())
			{
				command.CommandText = GetCommandForGettingCurrentVersion();

				object retrievedVersion = command.ExecuteScalar();
                if (retrievedVersion == null)
                    return string.Empty;

				return retrievedVersion.ToString();
			}
		}

		public void SetCurrentVersion(string currentVersion, IDbConnection connection)
		{
			if (connection == null)
				throw new ArgumentNullException("connection");

			// TODO Do we have to cast here? Is there a way to solve this with generics?
			using (SqlCommand command = ((SqlConnection) connection).CreateCommand())
			{
				command.CommandText = GetCommandForSettingVersion();
				command.Parameters.AddWithValue("@Version", currentVersion);

				command.ExecuteNonQuery();
			}
		}

		private static string GetCreateVersionsTableCommand()
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendLine("CREATE TABLE [dbo].[Versions] (");
			sb.AppendLine("    [Id] int IDENTITY(1, 1) NOT NULL,");
			sb.AppendLine("    [Version] int NOT NULL,");
			sb.AppendLine("    CONSTRAINT [PK_Versions] PRIMARY KEY CLUSTERED");
			sb.AppendLine("    (");
			sb.AppendLine("         [Id] ASC");
			sb.AppendLine("    ) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]");
			sb.AppendLine(") ON [PRIMARY]");

			return sb.ToString();
		}

		private static string GetInsertInitialVersionCommand()
		{
			return "INSERT INTO [Versions] ([Version]) VALUES (0)";
		}

		private static string GetCommandForGettingCurrentVersion()
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendLine("select top 1 [Version]");
			sb.AppendLine("from [dbo].[Versions]");
			sb.AppendLine("order by [Id] desc");

			return sb.ToString();
		}

		private static string GetCommandForSettingVersion()
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendLine("insert into [dbo].[Versions]");
			sb.AppendLine("    ([Version])");
			sb.AppendLine("values");
			sb.AppendLine("    (@Version)");

			return sb.ToString();
		}
	}
}